package com.s59.crm.clue.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.s59.crm.Result;
import com.s59.crm.clue.entity.Clue;
import com.s59.crm.clue.service.IClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 线索 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-06-28
 */
@RestController
@RequestMapping("/clues")
@RefreshScope
@CrossOrigin
public class ClueController {
    @Autowired
    private IClueService clueService;

    @Value("${server.port}")
    private int port;

    @Value("${clue.id.min}")
    private int min;

    @Value("${clue.id.max}")
    private int max;

    @Value("${cache.time}")
    private int timeOut;

    @SentinelResource(value="clues.get", blockHandler="blockGet")
    @GetMapping("/{id}")
    public Result<Clue> get(@PathVariable Long id) {
        System.out.println("port = " + port + ", min = " + min + ", max = " + max + ", timeOut = " + timeOut);
        if (id > max || id < min){
            throw new RuntimeException("id不在指定范围");
        }
        return Result.ok(clueService.getById(id));
    }

    public Result<Clue> blockGet(Long id, BlockException ex) {
        return Result.error("clues/get/{id}被限流了，请稍后访问");
    }
}